package com.sinosoft.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.orderinfo.OrderInfo;
import com.sinosoft.orderinfo.OrderInfoManager;
import com.sinosoft.shopcar.ShopToOrderUtil;
import com.sinosoft.shopcar.ShopcarManager;
import com.sinosoft.shopcar.Shopinfo;
import com.sinosoft.user.User;
import com.sinosoft.util.DisplayGetPage;
import com.sinosoft.util.PropertiesUtil;
import com.sinosoft.util.SystemConfigUtil;

@Controller
@RequestMapping("/orderinfo/orderform.html")
public class OrderInfoController extends BaseFormController {
	OrderInfoManager oimgr;
	ShopcarManager smgr;
	
	@Autowired
	public void setShopcarManager(ShopcarManager smgr)
	{
		this.smgr = smgr;
	}
	
	@Autowired
	public void setOrderInfoManager(OrderInfoManager oimgr)
	{
		this.oimgr = oimgr;
	}
	
	//订单管理
	@SuppressWarnings("unchecked")
	@RequestMapping(params="method=query")
	public ModelAndView showOrder(OrderInfo order,HttpServletRequest request,Model model)
	{
		List<OrderInfo> orderlist = null;
		User user =  (User) request.getSession().getAttribute(Constants.LOCAL_USER);
		Map<Long,String> authoriymap = user.getAuthoriy();
		Collection<String> authoriys = authoriymap.values();
		int isviewallorder = 0;//是否查看所有订单
		for (String s : authoriys) {
			if(s.equals(Constants.VIEW_ALL_ORDER))
			{
				isviewallorder = 1;
				break;
			}
		}
		if(isviewallorder!=1)
		{
			order.setUsername(user.getUsername());
		}
		String[] satellites = SystemConfigUtil.getSystemValue("satellite", this.getServletContext());
		model.addAttribute(Constants.LOCAL_SATELLITES,satellites);
		String page =  DisplayGetPage.getPageParamName("orders",request);//第几页
		if(page==null)
		{
			orderlist = oimgr.getOrderInfoList(order,0,Constants.pagesize);
		}
		else
		{
			orderlist = oimgr.getOrderInfoList(order,(Integer.parseInt(page)-1)*Constants.pagesize,Constants.pagesize);
		}
		if(order.getSatelliteid()!=null)
		{
			String[] sensors = SystemConfigUtil.getSystemValue(order.getSatelliteid(), this.getServletContext());
			model.addAttribute(Constants.LOCAL_SENSORS, sensors);
		}
		
		
		model.addAttribute("order",order);
		int resultSize =  oimgr.getOrderInfoCount(order);
		model.addAttribute("resultSize",resultSize);
		model.addAttribute("pagesize",Constants.pagesize);
		//产品类型
		String[] producttypes = PropertiesUtil.getPropertiesValue("orderinfo.producttypelist", request);
		Map<String,Object> producttypemap = getMap(producttypes);
		model.addAttribute("producttypemap",producttypemap);
		//优先级
		String[] prioritylist = PropertiesUtil.getPropertiesValue("shopcar.prioritylist", request);
		Map<String,Object> prioritylistmap = getMap(prioritylist);
		model.addAttribute("prioritylistmap",prioritylistmap);
		//订单类型
		String[] ordertypelist = PropertiesUtil.getPropertiesValue("shopcar.ordertypelist", request);
		Map<String,Object> ordertypelistmap = getMap(ordertypelist);
		model.addAttribute("ordertypelistmap",ordertypelistmap);
		//审核状态
		String[] checkstatus = PropertiesUtil.getPropertiesValue("order.checkstatus", request);
		Map<String,Object> checkstatusmap = getMap(checkstatus);
		model.addAttribute(Constants.CHECKSTATUS,checkstatusmap);
		//订单状态
		String[] orderstatus = PropertiesUtil.getPropertiesValue("order.orderstatus",request);
		Map<String,Object> orderstatusmap = getMap(orderstatus);
		model.addAttribute(Constants.ORDERSTATUS, orderstatusmap);
		//产品级别
		String[] productlevels = PropertiesUtil.getPropertiesValue("shopcar.productlevels", request);
		Map<String,Object> productlevelsmap = getMap(productlevels);
		model.addAttribute("productlevelsmap", productlevelsmap);
		double size = resultSize;
		model.addAttribute("displayallpage", Math.ceil(size / Constants.pagesize));
		model.addAttribute("thispage", Integer.parseInt(page));
		model.addAttribute("localuser",user.getUsername());
//		if(user!=null)
//		{
//			String[] ftpurls = SystemConfigUtil.getSystemValue("ftpurl", this.getServletContext());
//			String urlpre = "ftp://" + user.getUsername() + ":" + user.getPassword() + "@"+ftpurls[0] + ":" + user.getFtpport() +"/";
//			for (OrderInfo o : orderlist) {
//				o.setDownurl(urlpre+o.getProductuploaddir());
//			}
//		}
		return new ModelAndView("orderinfo/orderlist",Constants.ORDER_LIST,orderlist);
	}
	
	/**
	 * 添加到购物车
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params="method=reorder")
	public String reconOrderMethod(HttpServletRequest request,HttpServletResponse response)
	{
		String id = request.getParameter("id");
		OrderInfo orderinfo = null;
		if(id!=null && !"".equals(id))
			orderinfo = oimgr.getOrderInfoById(id);
		Shopinfo shopinfo = null;
		if(orderinfo!=null)
			shopinfo = ShopToOrderUtil.switchShopcar(orderinfo);
		smgr.saveShopcar(shopinfo);
		return "redirect:/orderinfo/orderform.html?method=query";
	}
	
	/**
	 * 订单详细信息
	 * @param map
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(params="method=details")
	public String ShowOrderinfo(ModelMap map,HttpServletRequest request)
	{
		String orderid = request.getParameter("orderid");
		OrderInfo o = oimgr.getOrderInfoById(orderid);
		map.addAttribute("orderinfo",o);
		//1、结算方式
		Map<String,Object> clearmap = new HashMap<String, Object>();
		String[] clearformlist = PropertiesUtil.getPropertiesValue("ordermodel.clearformvalue", request);
		clearmap = this.getMap(clearformlist);
		map.addAttribute("clearform",clearmap);
		//2、优先级别
		Map<String,Object> prioritymap = new HashMap<String, Object>();
		String[] prioritylist = PropertiesUtil.getPropertiesValue("shopcar.prioritylist", request);
		prioritymap = this.getMap(prioritylist);
		map.addAttribute("prioritylist",prioritymap);
		//6、多普勒质心计算方法
		Map<String,Object> fdcmap = new HashMap<String, Object>();
		String[] fdclist = PropertiesUtil.getPropertiesValue("ordermodel.fdclist", request);
		fdcmap = this.getMap(fdclist);
		map.addAttribute("fdclist",fdcmap);
		//7、多普勒频率计算方法
		Map<String,Object> fdrmap = new HashMap<String, Object>();
		String[] fdrlist = PropertiesUtil.getPropertiesValue("ordermodel.fdrlist", request);
		fdrmap = this.getMap(fdrlist);
		map.addAttribute("fdrlist",fdrmap);
		//8、天线方向图数据来源 
		Map<String,Object> antenmap = new HashMap<String, Object>();
		String[] antenlist = PropertiesUtil.getPropertiesValue("ordermodel.antenlist", request);
		antenmap = this.getMap(antenlist);
		map.addAttribute("antenlist",antenmap);
		//产品类型
		String[] producttypes = PropertiesUtil.getPropertiesValue("orderinfo.producttypelist", request);
		Map<String,Object> producttypemap = getMap(producttypes);
		map.addAttribute("producttypemap",producttypemap);
		//产品级别
		String[] productlevels = PropertiesUtil.getPropertiesValue("shopcar.productlevels", request);
		Map<String,Object> productlevelsmap = getMap(productlevels);
		map.addAttribute("productlevelsmap", productlevelsmap);
		//4、坐标系
		Map<String,Object> coordtypemap = new HashMap<String, Object>();
		String[] coordtypelist = PropertiesUtil.getPropertiesValue("ordermodel.coordtypelist", request);
		coordtypemap = this.getMap(coordtypelist);
		map.addAttribute("coordtypelist",coordtypemap);
		return "orderinfo/orderinfo";
	}
	
	@RequestMapping(params="method=payfor")
	public ModelAndView balanceOrder(Model model,HttpServletRequest request,HttpServletResponse response)
	{
		List<OrderInfo> orderlist = new ArrayList<OrderInfo>();
		String orderids = request.getParameter("orderid");
		if(orderids!=null && !orderids.isEmpty())
		{
			orderlist = oimgr.getOrderListByIds(orderids);
		}
		double price = 0;
		for (OrderInfo o : orderlist) {
			if(o.getPrice()!=null)
				price += o.getPrice();
		}
		model.addAttribute("price",price);
		return new ModelAndView("orderinfo/payfor","orderinfolist",orderlist);
	}
	
	/**
	 * 解析string数组，返回map对象
	 * @param str
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map getMap(String[] str)
	{
		Map<String,Object> map = new HashMap<String, Object>();
		for (int i = 0; i < str.length; i++) {
			String[] obj = str[i].split("_");
			if(obj.length>1)
				map.put(obj[1], obj[0]);
			else
				map.put(obj[0], obj[0]);
		}
		return map;
	}
	//删除订单
	@RequestMapping(params = "method=delete", method = RequestMethod.GET)
	public String delete(Long orderid,HttpServletRequest request){
		oimgr.removeOrderinfo(orderid);
		String page = request.getParameter("tz");
		String pagename = DisplayGetPage.getPageName("OrderInfo", request);
		return "redirect:/orderinfo/orderform.html?method=query";
	}
	
}
